<?php


App::uses('Component', 'Controller');
class BreadcrumbComponent extends Component
{

	/**
	 * breadcrumb
	 *
	 * @var array
	 */
	protected static $breadcrumb = array();

	/**
	 * HTML breadcrumb
	 *
	 * @var array
	*/
	protected static $template = array(
			'wrapper_start' => '<ul class="topicpath clearfix icoBase01">',
			'wrapper_end'   => ' </ul>',
			'item_start'    => '<li>',
			'anchor_class'  => 'btnBody',
			'span_start'    => '<span>',
			'span_end'      => '</span>',
			'item_end'      => '</li>',
	);

	//called before Controller::beforeFilter()
	public function __construct(ComponentCollection $collection, $settings = array()) {
		parent::__construct($collection, $settings);
	}

	/**
	 * Called before the Controller::beforeFilter().
	 *
	 * @param Controller $controller Controller with components to initialize
	 * @return void
	 * @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::initialize
	 */
	public function initialize(Controller $controller) {
		$this->controller = $controller;
	}

	/**
	 * get breadcumb
	 *
	 * @param number $index
	 */
	public static function get($index)
	{
		if (is_null($index) || $index < 0 || $index >= self::count())
		{
			return null;
		}

		return static::$breadcrumb[$index];
	}

	/**
	 * Set an item on breadcrumb static property
	 *
	 * @param string $title Display Link
	 * @param string $link Relative Link
	 * @param intenger $index Index to replace itens
	 * @return void
	 */
	public static function set($title, $link = '', $index = null, $overwrite = true)
	{
		// trim the bastard
		$title = trim($title);

		// if link is empty user the current
		$link = ($link === '') ? Router::url( $this->here, true ) : $link;

		if (is_null($index))
		{
			static::$breadcrumb[] = array(
					'title' => $title,
					'link'  => $link,
			);
		}
		else
		{
			if ($overwrite === true)
			{
				static::$breadcrumb[$index] = array(
						'title' => $title,
						'link'  => $link,
				);
			}
			else
			{
				static::append($title, $link, $index);
			}
		}
	}

	/**
	 * update node of breadcumb
	 *
	 * @param array $node ('title', 'link')
	 * @param number $index
	 */
	public static function update($node, $index)
	{
		if (is_null($index) || $index < 0 || $index >= self::count())
		{
			return;
		}

		static::$breadcrumb[$index] = $node;
	}

	/**
	 * Unset an item on breadcrumb static property
	 *
	 * @param intenger $index
	 * @return void
	 */
	public static function remove($index = null)
	{
		unset(static::$breadcrumb[$index]);
		static::sort_array(static::$breadcrumb);
	}

	/**
	 * Count number of items on breadcrumb
	 *
	 * @return int
	 */
	public static function count()
	{
		return count(static::$breadcrumb);
	}

	/**
	 * Create Html structure for Breadcrumb
	 *
	 * @return string The html
	 */
	public static function create_links()
	{
		if (empty(static::$breadcrumb) or count(static::$breadcrumb) < 1)
		{
			return '';
		}

		$template = static::$template;
		$output = $template['wrapper_start'];

		for ($i = 0, $total = static::count(); $i < $total; $i++)
		{
			$item = $template['span_start'].htmlspecialchars(static::$breadcrumb[$i]['title']).$template['span_end'];
			$url  = static::$breadcrumb[$i]['link'];
			// 最後尾もアンカーリンクあり
			$output .= $template['item_start'];
			//$output .= \Html::anchor(static::$breadcrumb[$i]['link'], $item, array('class' => $template['anchor_class']));
			$output .= '<a href="'. $url .'" class="'. $template['anchor_class'] .'">'. $item .'</a>';
			$output .= $template['item_end'];
		}

		$output .= $template['wrapper_end'];

		return $output;
	}

	/**
	 * Set an item on breadcrumb without overwrite the index
	 *
	 * @param string $title Display Link
	 * @param string $link Relative Link
	 * @param intenger $index Index to replace itens
	 * @return void
	 */
	protected static function append($title, $link = '', $index = null)
	{
		$breadcrumb = array();

		if (is_null($index) or $index > count(static::$breadcrumb) - 1)
		{
			static::set($title, $link);
		}

		for ($i = 0, $total = count(static::$breadcrumb); $i < $total; $i++)
		{
			if ($i === $index)
			{
				$breadcrumb[] = array(
					'title' => $title,
					'link'  => $link,
				);
				$i--;
				$index = null;
			}
			else
			{
				$breadcrumb[] = static::$breadcrumb[$i];
			}
		}

		static::$breadcrumb = $breadcrumb;
	}

	/**
	* Sort the index of an array
	*
	* @return &$array Array to sort
	*/
	protected static function sort_array(&$array)
	{
		if (is_array($array))
		{
			$aux = array();
			foreach ($array as $value)
			{
				$aux[] = $value;
			}

			$array = $aux;
		}
	}


}